﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;

namespace MyCpuMonitor.Classes.WMI
{
    public class PerfFormattedDataPerfOsProcessor
    {
        #region global vars

        private readonly ManagementObjectSearcher _mos;
        private readonly ManagementObjectCollection _moc;

        public int Count { get; set; }

        #endregion

        #region Ctor

        public PerfFormattedDataPerfOsProcessor()
        {
            _mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor");
            _moc = _mos.Get();

            Count = _moc.Count;
        }

        #endregion

        public string C1TransitionsPerSec(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["C1TransitionsPerSec"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string C2TransitionsPerSec(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["C2TransitionsPerSec"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string C3TransitionsPerSec(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["C3TransitionsPerSec"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Caption(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Caption"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Description(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Description"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string DPCRate(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["DPCRate"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string DPCsQueuedPerSec(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["DPCsQueuedPerSec"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Frequency_Object(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Frequency_Object"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Frequency_PerfTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Frequency_PerfTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Frequency_Sys100NS(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Frequency_Sys100NS"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string InterruptsPerSec(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["InterruptsPerSec"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Name(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Name"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentC1Time(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentC1Time"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentC2Time(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentC2Time"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentC3Time(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentC3Time"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentDPCTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentDPCTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentIdleTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentIdleTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentInterruptTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentInterruptTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentPrivilegedTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentPrivilegedTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentProcessorTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentProcessorTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string PercentUserTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["PercentUserTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Timestamp_Object(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Timestamp_Object"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Timestamp_PerfTime(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Timestamp_PerfTime"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }

        public string Timestamp_Sys100NS(int processor)
        {
            if ((processor + 1) > _moc.Count) processor = 0;

            var s = "";
            try
            {
                var c = 0;
                foreach (var o in _moc)
                {
                    if (c == processor)
                    {
                        s = o["Timestamp_Sys100NS"].ToString().Trim();
                        break;
                    }
                    c++;
                }
            }
            catch
            {
                s = "Unable to Determine";
            }

            return s;
        }




    }
}
